/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.mil.pamerj.projects.cardprinter.core;

import br.mil.pamerj.projects.cardprinter.dao.PessoaDAO;
import br.mil.pamerj.projects.cardprinter.dao.SOAPPessoaDAO;
import br.mil.pamerj.projects.cardprinter.dto.PessoaDTO;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fernandofot
 */
public class CardServiceImpl implements CardService {

    @Override
    public byte[] buildCard(PessoaDTO pessoa) {
        Rectangle pageSize = new Rectangle(162f, 255f);
        Document document = new Document(pageSize, 10f, 10f, 20f, 10f);
        System.out.println("width: " + PageSize.A8.getWidth());
        System.out.println("height: " + PageSize.A8.getHeight());
        Font font = new Font(Font.FontFamily.HELVETICA, 10);
        Font font2 = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD);
        ByteArrayOutputStream byteArray = null;
        try {
            byteArray = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, byteArray);
            document.open();
           
            
            Image background = Image.getInstance("http://localhost:8084/images/borda.png");
            background.scaleAbsoluteWidth(pageSize.getWidth());
           background.scaleAbsoluteHeight(pageSize.getHeight());
            background.setAbsolutePosition(0, 0);
            
            document.add(background);
            
           // document.newPage();
            
            Image logo = Image.getInstance("http://localhost:8084/images/logo.png");
            logo.scaleToFit(20, 20);
            logo.setAlignment(Element.ALIGN_LEFT);
           

            //document.add(logo);
            
            Paragraph header = null;

            header = new Paragraph();
            
            //header.add(logo);
           // Chunk unidade = new Chunk("PAME-RJ", font2);
            //unidade.setBackground(BaseColor.BLUE);
            //header.add(unidade);
            
            
            //header.setAlignment(Element.ALIGN_CENTER);
//            PdfPTable table = new PdfPTable(2);
//            table.setSpacingAfter(1f);
//            table.setSpacingBefore(1f);
//            table.setWidthPercentage(100);
//            table.addCell("");
//            table.addCell(new Phrase("PAME-RJ", font2));
//             document.add(header);
//            document.add(header);
//        document.add(header);     
         document.add(header);
            
       
            
            
            
            //document.add(logo);


            Image foto = Image.getInstance(pessoa.getFoto());
            foto.scaleToFit(115, 115);
            
            foto.setAlignment(Element.ALIGN_CENTER);
            foto.setSpacingBefore(300f);
            document.add(foto);

            
            
            Paragraph nome = null;

            nome = new Paragraph(pessoa.getCargo() + " " + pessoa.getNomeGuerra(), font);

            nome.setAlignment(Element.ALIGN_CENTER);

            nome.setSpacingAfter(1f);
            
            
            document.add(nome);
            

            
            Paragraph setor = null;

            setor = new Paragraph("DINF", font);

            setor.setAlignment(Element.ALIGN_CENTER);

            document.add(setor);
            
            
            document.close();

            return byteArray.toByteArray();



        } catch (BadElementException ex) {
            Logger.getLogger(CardServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(CardServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CardServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DocumentException ex) {
            Logger.getLogger(CardServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            
        }

        return null;

    }

    @Override
    public PessoaDTO findPessoa(Long id) {
        PessoaDAO dao = new SOAPPessoaDAO();
        return dao.find(id);
    }

    @Override
    public List<PessoaDTO> findAllPessoas() {
        PessoaDAO dao = new SOAPPessoaDAO();
        return dao.findAll();
    }
}
